package com.damai.action.quartz;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.quartz.SchedulerException;

import com.citywithincity.utils.ConfigReader;
import com.damai.action.quartz.annotation.QuartzJob;
import com.damai.jobcenter.QuartzPlugin;

public class QuartzJobBuilder {
	
	
	/**
	 * aop
	 * @param model
	 * @param methods
	 */
	public static void parse(Object model, Method[] methods) {
		
		for (Method method : methods) {
			if(method.isAnnotationPresent(QuartzJob.class)){
				QuartzJob job = method.getAnnotation(QuartzJob.class);
				Map<String, Object> data = new HashMap<String, Object>();
				QuartzMethodInfo info = new QuartzMethodInfo();
				info.setModel(model);
				info.setMethod(method);
				data.put(QuartzModelJob.JOB_INFO, info);
				String jobName = method.getClass().getSimpleName() + "_" + method.getName();
				String cronConfig = job.cron();
				String cron = ConfigReader.getDefault().get(cronConfig);
				try {
					QuartzPlugin.me().startJob(QuartzModelJob.class, cron, jobName, data);
				} catch (SchedulerException e) {
					throw new RuntimeException("启动自动任务失败！",e);
				}
				
			}
		}
		
	}
	
}
